package helloWorld;

import org.mt4j.MTApplication;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.shapes.MTEllipse;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;

public class MTRadialMenu extends MTEllipse {
	private MTApplication mtapp;
	float padding = 1;
	MTEllipse center;

	public MTRadialMenu(MTApplication mtapp, Vector3D centerPoint, float radius) {
		super(mtapp, centerPoint, radius, radius);
		this.mtapp = mtapp;
		center = new MTEllipse(mtapp, new Vector3D(mtapp.width / 2,
				mtapp.height / 2), 40, 40);
		center.removeAllGestureEventListeners();
		center.unregisterAllInputProcessors();
		center.registerInputProcessor(new TapProcessor(mtapp));
		center.addGestureListener(TapProcessor.class,
				new IGestureEventListener() {
					@Override
					public boolean processGestureEvent(MTGestureEvent ge) {
						TapEvent te = (TapEvent) ge;
						if (te.getId() == TapEvent.GESTURE_STARTED) {
							MTColor color = new MTColor(150, 150, 230, 200);
							center.setFillColor(color);
						} else if (te.getId() == TapEvent.GESTURE_ENDED) {
							MTColor color = new MTColor(150, 150, 200, 70);
							center.setFillColor(color);
						}
						return false;
					}
				});
		MTColor color = new MTColor(150, 150, 200, 70);
		center.setFillColor(color);
		addChild(center);
		setNoFill(true);
		createInnerLayer();
		createOuterLayer();
	}

	private void createInnerLayer() {
		for (int i = 0; i < 6; i++) {
			final MTAnnularSegment seg = new MTAnnularSegment(mtapp,
					mtapp.width / 2, mtapp.height / 2,
					center.getWidthXY(TransformSpace.GLOBAL) / 2 + padding,
					center.getWidthXY(TransformSpace.GLOBAL) * 2, i * 60,
					i * 60 + 60, 20);
			seg.removeAllGestureEventListeners();
			seg.unregisterAllInputProcessors();
			seg.registerInputProcessor(new TapProcessor(mtapp));
			seg.addGestureListener(TapProcessor.class,
					new IGestureEventListener() {
						@Override
						public boolean processGestureEvent(MTGestureEvent ge) {
							TapEvent te = (TapEvent) ge;
							if (te.getId() == TapEvent.GESTURE_STARTED) {
								MTColor color = new MTColor(150, 150, 230, 200);
								seg.setFillColor(color);
							} else if (te.getId() == TapEvent.GESTURE_ENDED) {
								MTColor color = new MTColor(150, 150, 200, 70);
								seg.setFillColor(color);
							}
							return false;
						}
					});
			MTColor color = new MTColor(150, 150, 200, 70);
			seg.setFillColor(color);
			seg.setStrokeColor(MTColor.BLACK);
			addChild(seg);
		}
	}

	private void createOuterLayer() {
		for (int i = 0; i < 4; i++) {
			final MTAnnularSegment seg = new MTAnnularSegment(mtapp,
					mtapp.width / 2, mtapp.height / 2,
					center.getWidthXY(TransformSpace.GLOBAL) * 5 / 2 + padding,
					center.getWidthXY(TransformSpace.GLOBAL) * 2, i * 90,
					i * 90 + 90, 20);
			seg.removeAllGestureEventListeners();
			seg.unregisterAllInputProcessors();
			seg.registerInputProcessor(new TapProcessor(mtapp));
			seg.addGestureListener(TapProcessor.class,
					new IGestureEventListener() {
						@Override
						public boolean processGestureEvent(MTGestureEvent ge) {
							TapEvent te = (TapEvent) ge;
							if (te.getId() == TapEvent.GESTURE_STARTED) {
								MTColor color = new MTColor(150, 150, 230, 200);
								seg.setFillColor(color);
							} else if (te.getId() == TapEvent.GESTURE_ENDED) {
								MTColor color = new MTColor(150, 150, 200, 70);
								seg.setFillColor(color);
							}
							return false;
						}
					});
			MTColor color = new MTColor(150, 150, 200, 70);
			seg.setFillColor(color);
			seg.setStrokeColor(MTColor.BLACK);
			addChild(seg);
		}
	}
}